package com.bw.product.service.impl;

import com.bw.product.domain.PmsSkuImages;
import com.bw.product.domain.PmsSkuInfo;
import com.bw.product.domain.PmsSpuInfoDesc;
import com.bw.product.domain.vo.*;

import com.bw.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private PmsSkuInfoService pmsSkuInfoService;
    @Autowired
    private PmsSkuImagesService pmsSkuImagesService;
    @Autowired
    private PmsSkuSaleAttrValueService pmsskuSaleAttrValueService;
    @Autowired
    private PmsSpuInfoDescService pmsSpuInfoDescService;
    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;

    @Autowired
    private SkuAttrDiscountCouponService skuAttrDiscountCouponService;

    @Autowired
    private ThreadPoolExecutor itemThreadPoolExcuter;
    @Override
    public ItemInfoVo findItemInfoBySkuId(Long skuId) throws ExecutionException, InterruptedException {
        ItemInfoVo itemInfoVo = new ItemInfoVo();

        CompletableFuture<PmsSkuInfo> pmsSkuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //查询sku详情
            PmsSkuInfo pmsSkuInfo = pmsSkuInfoService.selectPmsSkuInfoBySkuId(skuId);
            itemInfoVo.setSkuInfo(pmsSkuInfo);
            return pmsSkuInfo;
        }, itemThreadPoolExcuter);


        CompletableFuture<Void> listCompletableFuture = CompletableFuture.runAsync(() -> {
            //查询sku图片详情
            List<PmsSkuImages> pmsSkuImages = pmsSkuImagesService.selectPmsSkuImagesBySkuId(skuId);
            itemInfoVo.setSkuImagesList(pmsSkuImages);
        }, itemThreadPoolExcuter);


        CompletableFuture<Void> saleAttrVosCompletableFuture = CompletableFuture.runAsync(() -> {

            //             * 3.查spu相关信息销售属性 1s  查询sku对应的spu下所有的sku的属性
            List<SaleAttrVo> saleAttrVos=pmsskuSaleAttrValueService.selectSKuSaleAttrs(skuId);
            itemInfoVo.setSaleAttrs(saleAttrVos);

        }, itemThreadPoolExcuter);

        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> {
            //查询优惠券
            List<SkuAttrDiscountCouponVo> skuAttrDiscountCouponVos = skuAttrDiscountCouponService.selectSkuAttrDiscountCoupon(skuId);
            itemInfoVo.setSkuAttrDiscountCouponVos(skuAttrDiscountCouponVos);
        }, itemThreadPoolExcuter);


        CompletableFuture<Void> voidCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((pmsSkuInfo) -> {
//             * 4.查spu商品介绍 1s
            PmsSpuInfoDesc pmsSpuInfoDesc = pmsSpuInfoDescService.selectPmsSpuInfoDescBySpuId(pmsSkuInfo.getSpuId());
            itemInfoVo.setSpuInfoDesc(pmsSpuInfoDesc);

        }, itemThreadPoolExcuter);


        CompletableFuture<Void> voidCompletableFuture1 = pmsSkuInfoCompletableFuture.thenAcceptAsync((pmsSkuInfo) -> {

//             * 5.查规格和包装 05s
            List<AttrGroupVo> attrGroupVos = pmsAttrGroupService.selectAttrGroup(pmsSkuInfo.getCatalogId());
            itemInfoVo.setAttrGroupVos(attrGroupVos);

        }, itemThreadPoolExcuter);



        //阻塞 get()等待异步任务都执行完，阻塞释放
        CompletableFuture.allOf(
                pmsSkuInfoCompletableFuture,
                listCompletableFuture,
                saleAttrVosCompletableFuture,
                voidCompletableFuture,
                voidCompletableFuture1,
                voidCompletableFuture2
        ).join();


        return itemInfoVo;

    }
}


























